package com.app.bimo.commonbusiness.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.app.bimo.commonbusiness.R;
import com.app.bimo.commonbusiness.mvp.IPresenter;
import com.app.bimo.commonbusiness.mvp.IView;
import com.app.bimo.commonbusiness.util.DataUtil;
import com.app.bimo.commonbusiness.util.ToastUtils;
import com.app.bimo.commonbusiness.view.DialogUtil;
import com.app.bimo.commonbusiness.view.NetworkStateView;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static com.app.bimo.commonbusiness.base.BaseApplication.getContext;

public abstract class BaseFragment<P extends IPresenter> extends Fragment implements NetworkStateView.OnRefreshListener, IView {

    private NetworkStateView networkStateView;

    @Nullable
    protected P mPresenter;

    private Unbinder unbinder;

    private Dialog loadingDialg;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View mView =inflater.inflate(R.layout.layout_base_fragment, container, false);
        //加载子类Fragment的布局
        networkStateView = mView.findViewById(R.id.nsv_state_view);
        ConstraintLayout container2 = mView.findViewById(R.id.fl_activity_child_container);
        LayoutInflater.from(getContext()).inflate(setLayoutID(), container2,true);
        //绑定子控件
        unbinder = ButterKnife.bind(getActivity());
        return mView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        initData();
        initEvent();
        loadingDialg = DialogUtil.initLoadingDialog(getContext());
    }

    public abstract int setLayoutID();

    protected abstract void initView();

    protected abstract void initData();

    protected abstract void initEvent();

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    @Override
    public void onBaseRefresh() {

    }

    @Nullable
    public final <T extends View> T getView(@IdRes int id) {
        if (id == View.NO_ID) {
            return null;
        }
        return getView().findViewById(id);
    }


    //***************************************************************************//

    @Override
    public void showLogin() {

    }

    @Override
    public void showLoading() {
        networkStateView.showLoading();
    }

    @Override
    public void hideLoading() {
        networkStateView.showSuccess();
    }

    @Override
    public void showMessage(@NonNull String message) {
        if (!DataUtil.isEmpty(message)) {
            ToastUtils.showToast(getContext(), message);
        }
    }

    /**
     * 显示没有网络的布局
     */
    @Override
    public void showError() {
        networkStateView.showNoNetwork();
        networkStateView.setOnRefreshListener(this);
    }

    /**
     * 显示没有数据的布局
     */
    @Override
    public void showEmpty() {
        networkStateView.showEmpty();
        networkStateView.setOnRefreshListener(this);
    }

    @Override
    public void showDialogLoading() {
        loadingDialg.show();
    }

    @Override
    public void hideDialogLoading() {
        loadingDialg.dismiss();
    }

    @Override
    public Context getMContext() {
        return getContext();
    }



}
